package com.oms.servlet;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.oms.bean.RequestBean;

/*
GET方式：    URI？username=abc&password=123  username和password就是请求参数
POST方式：  请求正文中  username=abc&password=123

 */
@WebServlet("/Request2Map")
public class Request2Map extends HttpServlet {
	public Request2Map() {
		super();
	}
	public static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object name = request.getParameter("other");
		System.out.println(name);
		try {
			test7(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 实际开发中怎么玩：封装对象 借助第三方开发包 BeanUtils 终极版
	public void test7(HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
		response.setCharacterEncoding("UTF-8");
		RequestBean user = new RequestBean();
		System.out.println("封装前：" + user);
		// 表单的字段名和User中的属性名一致
		BeanUtils.populate(user, request.getParameterMap());
		System.out.println("封装后：" + user);
		
		response.getWriter().write("name="+user.getUsername()+"pass="+user.getPassword()+"phone="+user.getTelephone());
	}

	// 实际开发中怎么玩：封装对象 借助第三方开发包 BeanUtils
	public void test6(HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
		RequestBean user = new RequestBean();
		System.out.println("封装前：" + user);
		// 表单的字段名和User中的属性名一致
		Map<String, String[]> map = request.getParameterMap();// key：表单中input的name
		for (Map.Entry<String, String[]> me : map.entrySet()) {
			String paramName = me.getKey();
			String paramValues[] = me.getValue();
			BeanUtils.setProperty(user, paramName, paramValues);// 调用对应的setter方法
		}
		System.out.println("封装后：" + user);
	}

	// 实际开发中怎么玩：封装对象(技术点：反射+内省) 参考：java基础加强
	public void test5(HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
		RequestBean user = new RequestBean();
		System.out.println("封装前：" + user);
		// 表单的字段名和User中的属性名一致
		Map<String, String[]> map = request.getParameterMap();// key：表单中input的name
		for (Map.Entry<String, String[]> me : map.entrySet()) {
			String paramName = me.getKey();
			String paramValues[] = me.getValue();
			// 调用User中对应的写属性：比如当前username，需要调用setUsername(paramValues);
			PropertyDescriptor pd = new PropertyDescriptor(paramName, RequestBean.class);// 属性描述器：就是得getter或setter方法的类
			Method method = pd.getWriteMethod();// setter方法
			if (paramValues != null) {
				if (paramValues.length == 1) {
					method.invoke(user, paramValues);// 有一个数组（参考反射main方法）
				} else {
					method.invoke(user, (Object) paramValues);
				}
			}
		}
		System.out.println("封装后：" + user);
	}

	// 实际开发中怎么玩：封装对象(技术点：反射+内省) 参考：java基础加强
	public void test4(HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
		RequestBean user = new RequestBean();
		System.out.println("封装前：" + user);
		// 表单的字段名和User中的属性名一致
		Enumeration<?> e = request.getParameterNames();
		while (e.hasMoreElements()) {
			String paramName = (String) e.nextElement();// 每一个form中的input域的name
			String paramValues[] = request.getParameterValues(paramName);
			// 调用User中对应的写属性：比如当前username，需要调用setUsername(paramValues);
			PropertyDescriptor pd = new PropertyDescriptor(paramName, RequestBean.class);// 属性描述器：就是得getter或setter方法的类
			Method method = pd.getWriteMethod();// setter方法
			if (paramValues != null) {
				if (paramValues.length == 1) {
					method.invoke(user, paramValues);// 有一个数组（参考反射main方法）
				} else {
					method.invoke(user, (Object) paramValues);
				}
			}
		}
		System.out.println("封装后：" + user);
	}

	// 获取所有的请求参数
	public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Enumeration<?> e = request.getParameterNames();// 获取所有请求参数名
		while (e.hasMoreElements()) {
			String paramName = (String) e.nextElement();// 请求参数名
			String values[] = request.getParameterValues(paramName);
			out.write(paramName + "=");
			for (String v : values) {
				out.write(v + "，");
			}
			out.write("<br/>");
		}
	}

	// 为什么返回的是String或String[]:用户的所有输出都是字符串

	// 获取重名的请求参数
	public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String passwords[] = request.getParameterValues("password");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		for (String p : passwords)
			out.write(p + "<br/>");
	}

	// 获取单一的请求参数：重名的特例
	public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(username + ":" + password + ":" + code);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}